package com.aisile.crowd_funding01.system.controller;

import com.aisile.crowd_funding01.common.utils.R;
import com.aisile.crowd_funding01.system.pojo.SysMenu;
import com.aisile.crowd_funding01.system.pojo.SysMenuExample;
import com.aisile.crowd_funding01.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("sys/menu")
public class MenuController {
    public static final String VIEW_PATH = "system/menu/";
    @Autowired
    MenuService menuService;

    /**
     * 跳转菜单页面
     * @return
     */
    @RequestMapping
    public String menu(){
        return VIEW_PATH + "menu";
    }

    /**
     * 查询菜单列表
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public List<SysMenu> list(){
        return menuService.selectByExample(null);
    }

    /**
     * 添加跳转页面
     * @param parentId
     * @param model
     * @return
     */
    @RequestMapping("add")
    public String add(@RequestParam(value = "parentId",defaultValue="0") Long parentId, Model model){
        if (parentId==0){
            model.addAttribute("pName","根目录");
        }else{
            SysMenu sysMenu = menuService.selectByPrimaryKey(parentId);
            model.addAttribute("pName",sysMenu.getName());
        }
        model.addAttribute("parentId",parentId);
        return VIEW_PATH + "add";
    }

    /**
     * 添加
     * @param sysMenu
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public R save(SysMenu sysMenu){
        int insert = menuService.insert(sysMenu);
        if(insert>0){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 跳转修改页面
     * @param menuId
     * @param model
     * @return
     */
    @RequestMapping("edit")
    public String edit(Long menuId,Model model){
        SysMenu sysMenu = menuService.selectByPrimaryKey(menuId);
        if(sysMenu.getParentId()==0){
            model.addAttribute("pName","根目录");
        }else{
            SysMenu sysMenu1 = menuService.selectByPrimaryKey(sysMenu.getParentId());
            model.addAttribute("pName",sysMenu1.getName());
        }
        model.addAttribute("menu",sysMenu);
        return VIEW_PATH + "edit";
    }

    /**
     * 修改菜单
     * @param sysMenu
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public R update(SysMenu sysMenu){
        int i = menuService.updateByPrimaryKey(sysMenu);
        if(i>0) {
            return R.ok();
        }else{
            return R.error();
        }
    }

    /**
     * 删除菜单
     * @param menuId
     * @return
     */
    @RequestMapping("del")
    @ResponseBody
    public R del(Long menuId){
        int i = menuService.selectChildCount(menuId);
        if(i>0){
            //如果有子节点，请先删除子节点
            return R.error("请先删除子节点！");
        }else{
            int i1 = menuService.deleteByPrimaryKey(menuId);
            if(i1>0){
                return R.ok();
            }
        }
        return R.error();
    }
}
